package com.sto.stoapp.util;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sto.stoapp.db.StoFilePreference;
import com.sto.stoapp.entity.BillControls;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by wj on 2016-12-29.
 */

public class BillControlsUtil {

    public static void main(String[] args){
        String time=get6MothAgoTime();
        System.out.print(time);
    }

    /**
     * 获取6个月之前的时间
     */
    public static String get6MothAgoTime(){

        Calendar calendar=Calendar.getInstance();
        calendar.add(Calendar.MONTH,-6);

        Date time=calendar.getTime();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String timeStr=sdf.format(time);

        return timeStr;
    }

    /**
     * 获取6个月之后的时间
     * @param date
     * @return
     */
    public static Date get6AfterDate(Date date){

        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);

        calendar.add(Calendar.MONTH,6);

        return calendar.getTime();
    }

    /**
     * 字符串解析为对象
     * @param entity
     * @return
     */
    public static BillControls str2Entity(String entity){
        try{
            Gson gson=new Gson();
            BillControls billControls=gson.fromJson(entity,BillControls.class);

            return billControls;
        }catch (Exception e){
            return null;
        }

    }

    /**
     * 将列表数据转换为字符串
     * @param datas
     * @return
     */
    public static String list2Str(List<BillControls.RulesBean> datas){
        Gson gson=new Gson();
        String rules=gson.toJson(datas);
        return rules;
    }

    /**
     * 将字符串转换为列表数据
     * @param rules
     * @return
     */
    public static List<BillControls.RulesBean> str2List(String rules){

        Gson gson=new Gson();
        return gson.fromJson(rules,new TypeToken<List<BillControls.RulesBean>>(){}.getType());
    }

    /**
     * 过滤旧数据
     * @param datas
     * @return
     */
    public static List<BillControls.RulesBean> fillterList(List<BillControls.RulesBean> datas){

        List<BillControls.RulesBean> list=new ArrayList<BillControls.RulesBean>();
        list.addAll(datas);

        for (BillControls.RulesBean data : datas) {
            if(compareTime(data.getEndTime())){
                list.remove(data);
            }
        }

        return list;
    }

    /**
     * 时间是否过期,true 过期
     * @param endTime
     * @return
     */
    public static boolean compareTime(String endTime){

        if(null==endTime)
            return false;

        try {
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date endDate=sdf.parse(endTime);
            endDate=get6AfterDate(endDate);
            Date now=new Date();
            if(now.after(endDate)){
               return true;
            }else {
                return false;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return false;
    }

    /**
     * 判断单号是否属于管控范围，是true,否false
     * @param bill
     * @return
     */
    public static boolean isControls(Context context ,String bill){

        if(null==bill || "".equals(bill)){
            return false;
        }

        double billNo=Double.valueOf(bill);

        //获取本地管控规则
        String oldRules= StoFilePreference.getInstance(context).getControlsRules();
        List<BillControls.RulesBean> list = BillControlsUtil.str2List(oldRules);

        for (BillControls.RulesBean bean : list) {
            double startBill=Double.valueOf(bean.getStartNo());
            double endBill=Double.valueOf(bean.getEndNo());
            if(billNo<=endBill && billNo>=startBill){
                //如果规则未过期
                return false;
            }
        }

        return true;
    }

}
